/**
 * Created by Administrator on 2017/5/11 0011.
 */

//很多子弹
//6.创建子弹
function Bullet() {
    this.id = parseInt(Math.random() * 10000) + "";
    this.ele = null;

    this.init = function () {
        this.ele = document.createElement("div");   //创建子弹并添加到场景中
        gameEngine.ele.appendChild(this.ele);
        gameEngine.bullets[this.id] = this;  //把当前的单个子弹对象，添加的游戏引擎里的子弹结合里面
        this.ele.className = "bullet";
        this.ele.style.left = myPlane.ele.offsetLeft + parseInt((myPlane.ele.offsetWidth - this.ele.offsetWidth) / 2) + "px";
        this.ele.style.top = myPlane.ele.offsetTop - this.ele.offsetHeight + "px";
        //console.log(gameEngine.bullets.length);
        return this;

    }

    //7.子弹自己的移动方法
    this.move = function (xspeed) {
        var that = this;
        this.ele.timer = setInterval(function () {
            if (that.ele.offsetTop < -18 || that.ele.offsetLeft < 0 || that.ele.offsetLeft > gameEngine.ele.offsetWidth - that.ele.offsetWidth) {
                that.ele.remove();                      //清除超出游戏区域的子弹对象
                clearInterval(that.ele.timer);          //关闭超出游戏区域的子弹对象的定时器
                delete gameEngine.bullets[that.id]; //将超出游戏区域的子弹对象从allBullets中删除
            } else {
                that.ele.style.top = that.ele.offsetTop - 10 + "px";
                that.ele.style.left = that.ele.offsetLeft + xspeed + "px";
               // console.log(that.ele.offsetLeft);
            }
        }, 30);
    }

    //子弹爆炸
    this.boom = function () {
        clearInterval(this.ele.timer); //停止移动
        //爆炸动画
        this.ele.className = "bullet-die";
        var that = this;
        var arr = ["img/die1.png", "img/die2.png"];
        var i = 0;
        var boomTimer = setInterval(function () {
            if (i >= 2) {
                clearInterval(boomTimer); //清除定时器
                that.ele.remove(); //移除节点
                delete gameEngine.bullets[that.id]; //将敌机对象从allEnemys中删除
            }
            else {
                that.ele.style.backgroundImage = "url(" + arr[i++] + ")";
            }
        }, 100);

    }

}